<template>
  <div class="login-container">
    <div class="loginBox">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on"
        class="login-form" label-position="left">
        <div class="title-container clearfix">
          <div class="title">
            <em class="logo"></em>
            <span class="span_title">用户登录</span>
            <Lang class="right-menu-item" />
          </div>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="Username"
            name="username" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password"
            :type="passwordType" placeholder="Password" name="password" tabindex="2"
            auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin">{{$t('login.login')}}</el-button>
        <!-- <div class="tips">
          <span style="margin-right:20px;">username: root@admin.com</span>
          <span> password: 123456</span>
        </div> -->
      </el-form>
    </div>

  </div>
</template>

<script>
import { validEmail } from '@/utils/validate';
import shajs from 'jssha';
import { mapActions } from 'vuex';
export default {
  name: 'Login',
  data() {
    const validateEmail = (rule, value, callback) => {
      if (!validEmail(value)) {
        callback(new Error('输入的邮箱格式不正确'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: 'root@admin.com',
        password: '123456',
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '邮箱不能为空' },
          {
            validator: validateEmail,
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    //引用vuex login方法
    ...mapActions(['user/login']),
    //密码是否能看见
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        this.loading = true;
        let pwd = new shajs('SHA-256', 'TEXT');
        pwd.update(this.loginForm.password);
        var hash = pwd.getHash('HEX');
        console.log(hash);
        if (!valid) {
          this.$message.error('登录失败');
          return false;
        }
        //校验通过
        try {
          await this['user/login']({
            username: this.loginForm.username,
            password: hash,
          });
          this.$message.success('登录成功');
          this.$router.push('/');
        } catch (err) {
          console.dir(err);
        } finally {
          this.loading = false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$light_gray: #000;
$cursor: #000;
$bg: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 5px;
      line-height: 36px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  min-height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
  background: url('~@/assets/common/backgroundcolor.jpg') no-repeat 100% 100%;
  background-position: center; // 将图片位置设置为充满整个屏幕
  background-attachment: fixed;
  background-size: cover;

  .loginBox {
    width: 100%;
    min-height: 720px;
    background: url('~@/assets/common/background.png') no-repeat 100% 100%; // 设置背景图片
  }
  .login-form {
    position: absolute;
    top: 50%;
    left: 20%;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: -200px 0 0 0;
    overflow: hidden;
    background: #f5f5f5;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    // float: left;
    color: black;
    .clearfix:after {
      display: block;
      clear: both;
      content: '';
      visibility: hidden;
      height: 0;
    }
    .clearfix {
      zoom: 1;
    }
    .title {
      font-size: 22px;
      color: $light_gray;
      padding: 0 10px;
      text-align: center;
      padding-bottom: 15px;
      float: left;
      // font-weight: bold;
    }
    .logo {
      display: inline-block;
      width: 35px;
      height: 26px;
      float: left;
      background: url('~@/assets/common/logo.png');
    }
    .right-menu-item {
      position: absolute;
      top: 5px;
      right: 0;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
